<?php

namespace Admin\Controller;

class ProductController extends AdminbaseController {

    function _initialize() {
        parent::_initialize();
        $this->currentModel = D("Product");
    }

    //首页
    public function index() {

        $requestMap = $this->convertRequestToMap();

        //分页
        $count = $this->currentModel->getListCount($requestMap);
        $page = $this->page($count, C("DEFAULT_PAGE_SIZE"));

        //获取列表
        $list = $this->currentModel->queryForList($page, $requestMap);

        $this->assign("statusEnum", C("DEFAULT_YN"));
        $this->assign('list', $list);
        $this->assign("page", $page->show('Admin', $this->parameter));
        $this->display();

    }

    public function addDesc() {
        $productId = $_REQUEST["id"];
        $this->assign("productId", $productId);
        $this->display("addDesc");
    }

    public function removeDesc() {

        $id = $_REQUEST['id'];

        $productDesc = D("ProductDesc")->queryById($id);

        $productId = $productDesc["product_id"];

        D("ProductDesc")->deleteById($id);

        $this->success('删除成功!', U("product/desc", array("id"=>$productId)));

    }

    public function desc() {

        $id = $_REQUEST["id"];

        $product = D("Product")->queryById($id);

        $list = D("ProductDesc")
            ->where(array("product_id"=>$id))
            ->order("desc_id desc")
            ->select();

        $this->assign("product", $product);
        $this->assign("productId", $id);
        $this->assign('list', $list);
        $this->display();

    }

    public function editDesc() {
        $id = $_REQUEST["id"];
        $desc = D("ProductDesc")->where(array("desc_id"=>$id))->find();
        $this->assign("info", $desc);
        $this->display("editDesc");
    }

    public function addDescPost() {

        $data["product_id"] = $_REQUEST["productId"];
        $data["create_time"] = getCurrentDate();
        $data["title"] = $_REQUEST["title"];
        $data["second_title"] = $_REQUEST["secondTitle"];
        $data["title_image"] = $_REQUEST["titleImage"];
        $data["photo_list"] = $_REQUEST["photoJson"];
        $data["content"] = html_entity_decode($_REQUEST["description"]);

        D("ProductDesc")->add($data);

        $this->success("添加成功", U("product/desc", array("id"=>$_REQUEST["productId"])));

    }

    public function editDescPost() {

        $id = $_REQUEST["id"];

        $data = D("ProductDesc")->queryById($id);

        $data["title"] = $_REQUEST["title"];
        $data["second_title"] = $_REQUEST["secondTitle"];
        $data["title_image"] = $_REQUEST["titleImage"];
        $data["photo_list"] = $_REQUEST["photoJson"];
        $data["content"] = html_entity_decode($_REQUEST["description"]);

        D("ProductDesc")->updateInfo($data);

        $this->success("更新成功", U("product/desc", array("id"=>$data["product_id"])));

    }

    public function add() {
        $this->assign("statusEnum", C("GROUP_PRODUCT_STATUS"));
        $this->display("add");
    }
    /**
     * 添加数据
     */
    public function addPost() {

        $count = D("Product")
            ->where(array("category_id"=>$_REQUEST["categoryId"]))
            ->count();

//        if($count >= 1) {
//            $this->error("对不起，目前一种疾病只能有一个商品");
//            exit();
//        }

        $data["product_name"] = $_REQUEST["productName"];
        $data["description"] = html_entity_decode($_REQUEST["description"]);
        $data["create_time"] = getCurrentDate();
        $data["category_id"] = $_REQUEST["categoryId"];
        $data["price"] = $_REQUEST["price"];
        $data["image_url"] = $_REQUEST["imageUrl"];
        $data["need_measure"] = $_REQUEST["needMeasure"];
        $data["measure_json"] = serialize(json_decode(html_entity_decode(
            $_REQUEST["measureJson"]), true));
        $data["measure_image"] = $_REQUEST["measureImage"];
        $data["measure_description"] = html_entity_decode($_REQUEST["measureDescription"]);
        $data["count"] = 0;

        $data["group_status"] = $_REQUEST["groupStatus"];
        if(isNullOrEmpty($data["group_status"])){
            $data["group_price"] = null;
            $data["group_code"] = null;
            $data["expiry_date"] = null;
        }else{
            $data["group_price"] = $_REQUEST["groupPrice"];
            $data["group_code"] = $_REQUEST["groupCode"];
            $data["expiry_date"] = $_REQUEST["expiryDate"];
        }

        $this->currentModel->add($data);

        $this->success("添加成功", U("product/index"));

    }

    public function edit() {
        $id = $_REQUEST["id"];
        $info = $data = $this->currentModel->queryById($id);
        $this->assign("info", $info);
        $this->assign("statusEnum", C("GROUP_PRODUCT_STATUS"));
        $this->display("edit");
    }

    /**
     * 更新数据
     */
    public function editPost() {

        $id = $_REQUEST["id"];

        $data = $this->currentModel->queryById($id);
        $data["product_name"] = $_REQUEST["productName"];
        $data["description"] = html_entity_decode($_REQUEST["description"]);
        $data["category_id"] = $_REQUEST["categoryId"];
        $data["price"] = $_REQUEST["price"];
        $data["image_url"] = $_REQUEST["imageUrl"];
        $data["need_measure"] = $_REQUEST["needMeasure"];
        $data["measure_json"] = serialize(json_decode(html_entity_decode(
            $_REQUEST["measureJson"]), true));
        $data["measure_image"] = $_REQUEST["measureImage"];
        $data["measure_description"] = html_entity_decode($_REQUEST["measureDescription"]);

        $data["group_status"] = $_REQUEST["groupStatus"];
        if(isNullOrEmpty($data["group_status"])){
            $data["group_price"] = null;
            $data["group_code"] = null;
            $data["expiry_date"] = null;
        }else{
            $data["group_price"] = $_REQUEST["groupPrice"];
            $data["group_code"] = $_REQUEST["groupCode"];
            $data["expiry_date"] = $_REQUEST["expiryDate"];
        }

        $this->currentModel->updateInfo($data);

        $this->success("更新成功", U("product/index"));

    }

    public function measureJson() {

        $id = I("post.id");
        $info = $this->currentModel->queryById($id);

        echo json_encode(unserialize($info["measure_json"]));

    }

}